﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using Ledict.Utility.Common;
using System.Windows.Forms;
using System.Xml;
using Ledict.Utility.Helper;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    /// <summary>
    /// 对casecause案由表的相关操作
    /// </summary>
    public class CaseCauseDAL
    {
        private string DbName;

        private string CommonDbName;
        public CaseCauseDAL()
        {
            DbName = "JudgeDoc";
            CommonDbName = "JudgeProcess";
        }
        TreeDateDAL treedal = new TreeDateDAL();

        public void ShowData()
        {
            
            string conn = ConfigHelper.ConnectionStrings("JudgeDoc").ConnectionString;
               // IFreeSql freeSql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.SqlServer,conn).Build();
            IFreeSql freeSql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.SqlServer, conn, typeof(FreeSql.SqlServer.SqlServerProvider<>)).Build();

            List<string> databaselist= freeSql.DbFirst.GetDatabases();
        }
        
        
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public string Exists(string CC_Name, string CC_ParentID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"	select CC_ID from dbo.SYS_CaseCause
	       where CC_Name=@CC_Name
	       and CC_ParentID=@CC_ParentID ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_Name", DbType.String, CC_Name);
            oBase.AddInParameter(oCmd, "@CC_ParentID", DbType.String, CC_ParentID);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));

        }

        //public bool IsExistsCCName(string cc_Name)
        //{
        //    string sql = string.Format(@" select count(1) from  ");
        //}

        /// <summary>
        /// 获得数据列表(所有下级)
        /// </summary>
        public DataTable GetAllChild(string CC_ID)
        {
            string strSql = string.Format(@" select * FROM SYS_CaseCause where  CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_ID<>'{0}' ", CC_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获得数据列表(所有下级)
        /// </summary>
        public DataTable GetAllChildNew(string CC_ID)
        {
            string strSql = string.Format(@" select * FROM SYS_CaseCause where  CHARINDEX(',{0},',','+AllParentId+',')>0  ", CC_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }




        /// <summary>
        /// 获得数据列表(所有下级)
        /// </summary>
        public DataTable GetAllChild1(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * FROM SYS_CaseCause 
            where ','+AllParentId+',' like '%,'+@CC_ID+',%' ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获得信息单元编号（判决书）
        /// </summary>
        /// <param name="ccId">情形情节ID</param>
        /// <param name="purposeId">用途ID，默认为空</param>
        public string GetRegCodeByJudgeBook(string ccId, string purposeId = null)
        {
            StringBuilder strSql = new StringBuilder();
            if (string.IsNullOrEmpty(purposeId))
            {
                //徐仁娜版本，按照ccid和默认的判决书类型，获取正则
                strSql.Append(@"select CC_RegexInfoCode FROM 
                                dbo.SYS_Express_Source 
                                where CC_ID=@CaseCauseID
                                and (Source_ID='JudgeBook' or Source_ID='a12101')");

            }
            else  //todo by zhangpeng:2016-12-26 按照王晓丽新需求，民事需要按照用途筛选出指定的正则
            {
                strSql.Append(@"select CC_RegexInfoCode FROM 
                                dbo.SYS_Express_Source 
                                where CC_ID=@CaseCauseID
                                and ES_Purpose=@purposeId");
            }

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CaseCauseID", DbType.String, ccId);
            oBase.AddInParameter(oCmd, "@purposeId", DbType.String, purposeId);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));

        }

        /// <summary>
        /// 获得信息单元编号（判决书）
        /// </summary>
        /// <param name="ccId">情形情节ID</param>
        /// <param name="purposeId">用途ID，默认为空</param>
        public DataTable GetRegCodeByJudgeBook1(string ccId)
        {
            string strSql = string.Format(@" select CC_RegexInfoCode,b.EI_ID FROM  dbo.SYS_Express_Source a left join LdtCommonManage.dbo.Reg_InfoUnit b on a.CC_RegexInfoCode=b.EI_Code  where CC_ID='{0}' and (Source_ID='JudgeBook' or Source_ID='a12101') ", ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }

        }

        /// <summary>
        /// 根据ID获取他的父级ID
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string GetParentIDByID(string ccID)
        {
            string sql = string.Format(@" select CC_ParentID from SYS_CaseCause where CC_ID = '{0}' ", ccID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool GetCCIDByCC_Name(string CC_Name, string CC_ParentName)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select Count(*)from dbo.SYS_CaseCause as a 
           where CC_Name=@CC_Name and (select CC_Name 
           from SYS_CaseCause where CC_ID =a.CC_ParentID)=@CC_ParentName ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_Name", DbType.String, CC_Name);
            oBase.AddInParameter(oCmd, "@CC_ParentName", DbType.String, CC_ParentName);
            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 表述语句中是否存在该记录
        /// </summary>
        public bool IsExistsData(SYS_Express_Word WordModel)
        {
            string str = string.Format(@" select COUNT(1) from dbo.SYS_Express_Word_Process where EW_CaseId='{0}' and MS_ID in(select CC_ID FROM Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where ','+AllParentId+',' like '%,'+'{1}'+',%' and CC_ID<>'{1}') ", WordModel.EW_CaseId, WordModel.MS_ID);
            Database oBase = DatabaseFactory.CreateDatabase(CommonDbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(str.ToString());
            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddSYS_CaseCause(SYS_CaseCause Model, string username)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_AddSYS_CaseCause");

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Model.CC_ID);
            oBase.AddInParameter(oCmd, "@CC_Name", DbType.String, Model.CC_Name);
            oBase.AddInParameter(oCmd, "@CC_ParentID", DbType.String, Model.CC_ParentID);
            oBase.AddInParameter(oCmd, "@CC_NodeType", DbType.String, Model.CC_NodeType);
            oBase.AddInParameter(oCmd, "@isLeafNode", DbType.String, Model.isLeafNode);
            oBase.AddInParameter(oCmd, "@XmlPath", DbType.String, Model.XmlPath);
            oBase.AddInParameter(oCmd, "@CC_IsProcessNode", DbType.String, Model.CC_IsProcessNode);
            oBase.AddInParameter(oCmd, "@CC_IsCombination", DbType.String, Model.CC_IsCombination);
            oBase.AddInParameter(oCmd, "@CC_CommonCase", DbType.String, Model.CC_CommonCase);
            oBase.AddInParameter(oCmd, "@CC_SerialNumb", DbType.Double, Model.CC_SerialNumb);
            oBase.AddInParameter(oCmd, "@CC_NodeSubType", DbType.String, Model.CC_NodeSubType);
            oBase.AddInParameter(oCmd, "@AllParentId", DbType.String, Model.AllParentId);
            oBase.AddInParameter(oCmd, "@AllParentName", DbType.String, Model.AllParentName);
            oBase.AddInParameter(oCmd, "@HW_level", DbType.Double, Model.HW_level);
            oBase.AddInParameter(oCmd, "@CC_IsBasicElements", DbType.Int32, Model.CC_IsBasicElements);
            oBase.AddInParameter(oCmd, "@PIS_ShowName", DbType.String, Model.PIS_ShowName);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                OperLogDAL.AddOperLog(username, "insert", "SYS_CaseCause", "CC_ID=" + Model.CC_ID + "xmlPath" + Model.XmlPath);
                return true;
            }
            return false;
        }


        public double ComputeSerialNumb(string CC_ID, string CC_IDNext)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" 

           if(@CC_IDNext='')
           begin
            declare  @count int 
            select @count=Count(*)
            from SYS_CaseCause where isnull(CC_ParentID,'')=@CC_ID
            if(@count>0)
            select MAX( isnull(CC_SerialNumb,0))+1 
            from dbo.SYS_CaseCause where isnull(CC_ParentID,'')=@CC_ID
            else
            select 1.0 as CC_SerialNumb

           end
           else
           select avg( isnull(CC_SerialNumb,0))
           from dbo.SYS_CaseCause where 
           CC_ID =@CC_ID or  CC_ID=@CC_IDNext
	");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            oBase.AddInParameter(oCmd, "@CC_IDNext", DbType.String, CC_IDNext);

            return Convert.ToDouble(oBase.ExecuteScalar(oCmd));

        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateSYS_CaseCause(SYS_CaseCause Model, string username)
        {
            //DataTable dt = GetSYS_CaseCauseByCCID(Model.CC_ID);
            //if (dt != null && dt.Rows.Count > 0)
            //{
            //    SYS_CaseCause sc = IsParentRealation1(Model.CC_ID);
            //    string xml = sc.ConfigXML;
            //    if (!string.IsNullOrEmpty(xml))
            //    {
            //        xml = xml.Replace(dt.Rows[0]["XmlPath"].ToString(), Model.XmlPath);
            //        UpdateConfigXML(sc.CC_ID, xml);
            //    }
            //}
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@" update [SYS_CaseCause]
	       set CC_Name=@CC_Name,
	       CC_ParentID=@CC_ParentID,
	       CC_NodeType=@CC_NodeType,
	       isLeafNode=@isLeafNode,
	       XmlPath=@XmlPath,
	       CC_IsCombination=@CC_IsCombination,
	       CC_IsProcessNode=@CC_IsProcessNode,
	       CC_CommonCase=@CC_CommonCase,
           CC_SerialNumb=@CC_SerialNumb,
           CC_DevelopSchedule=@CC_DevelopSchedule,
           CC_ExpressElement=@CC_ExpressElement,
            CC_NodeSubType=@CC_NodeSubType,
            AllParentId=@AllParentId,
            AllParentName=@AllParentName,
            HW_level=@HW_level,
            MC_influenceTypeID=@MC_influenceTypeID,
            CC_IsBasicElements=@CC_IsBasicElements,
            PIS_ShowName=@PIS_ShowName,
            CC_District=@CC_District,
            IsML=@IsML
	       where CC_ID=@CC_ID
	");

            if (OperLogDAL.AddOperLog(username, "update", "SYS_CaseCause", "CC_ID=" + Model.CC_ID + " XMLPATH：" + Model.XmlPath))
            {

                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
                oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Model.CC_ID);
                oBase.AddInParameter(oCmd, "@CC_Name", DbType.String, Model.CC_Name);
                oBase.AddInParameter(oCmd, "@CC_ParentID", DbType.String, Model.CC_ParentID);
                oBase.AddInParameter(oCmd, "@CC_NodeType", DbType.String, Model.CC_NodeType);
                oBase.AddInParameter(oCmd, "@isLeafNode", DbType.String, Model.isLeafNode);
                oBase.AddInParameter(oCmd, "@XmlPath", DbType.String, Model.XmlPath);
                oBase.AddInParameter(oCmd, "@CC_IsProcessNode", DbType.String, Model.CC_IsProcessNode);
                oBase.AddInParameter(oCmd, "@CC_IsCombination", DbType.String, Model.CC_IsCombination);
                oBase.AddInParameter(oCmd, "@CC_CommonCase", DbType.String, Model.CC_CommonCase);
                oBase.AddInParameter(oCmd, "@CC_SerialNumb", DbType.String, Model.CC_SerialNumb);
                oBase.AddInParameter(oCmd, "@CC_DevelopSchedule", DbType.String, Model.CC_DevelopSchedule);
                oBase.AddInParameter(oCmd, "@CC_ExpressElement", DbType.String, Model.CC_ExpressElement);
                oBase.AddInParameter(oCmd, "@CC_NodeSubType", DbType.String, Model.CC_NodeSubType);
                oBase.AddInParameter(oCmd, "@AllParentId", DbType.String, Model.AllParentId);
                oBase.AddInParameter(oCmd, "@AllParentName", DbType.String, Model.AllParentName);
                oBase.AddInParameter(oCmd, "@HW_level", DbType.Int32, Model.HW_level);
                oBase.AddInParameter(oCmd, "@MC_influenceTypeID", DbType.String, Model.MC_influenceTypeID);
                oBase.AddInParameter(oCmd, "@CC_IsBasicElements", DbType.Int32, Model.CC_IsBasicElements);
                oBase.AddInParameter(oCmd, "@PIS_ShowName", DbType.String, Model.PIS_ShowName);
                oBase.AddInParameter(oCmd, "@CC_District", DbType.String, Model.CC_District);
                oBase.AddInParameter(oCmd, "@IsML", DbType.Int32, Model.IsML);                
                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 更新AllParentId
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool CaseCauseUpdateAllPid(string CC_ID)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_CaseCauseUpdateAllPid");
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public DataTable GetSYS_CaseCauseByXmlPath(string XmlPath)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select Top 1 * from dbo.SYS_CaseCause 
	         where XmlPath=@XmlPath");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@XmlPath", DbType.String, XmlPath);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取CC_IDs信息
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public DataTable GetSYS_CaseCauseByCCID(string ccId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select Top 1 * from dbo.SYS_CaseCause 
	         where CC_ID=@CC_ID");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, ccId);
            DataTable otB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (otB.Rows.Count > 0)
            {
                return otB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYS_CaseCause GetCaseCauseByCcid(string ccId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from dbo.SYS_CaseCause  ");
            strSql.Append(" where CC_ID=@CC_ID ");
            SYS_CaseCause model = new SYS_CaseCause();
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, ccId);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return DataRowToModel(OtB.Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public string GetCauseConfigXML(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
            select top 1 ConfigXML from dbo.SYS_CaseCause
            where  ( CC_NodeType=2 or  CC_NodeType=23)  and  ','+( select AllParentId  from dbo.SYS_CaseCause
            where CC_ID='" + @CC_ID + @"' )+',' like '%,'+CC_ID+',%'
            order by allparentid desc");
            SYS_CaseCause model = new SYS_CaseCause();
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYS_CaseCause DataRowToModel(DataRow row)
        {
            SYS_CaseCause model = new SYS_CaseCause();
            if (row != null)
            {
                if (row["CC_ID"] != null)
                {
                    model.CC_ID = row["CC_ID"].ToString();
                }
                if (row["CC_Name"] != null)
                {
                    model.CC_Name = row["CC_Name"].ToString();
                }
                if (row["CC_Code"] != null)
                {
                    model.CC_Code = row["CC_Code"].ToString();
                }
                if (row["CC_ParentID"] != null)
                {
                    model.CC_ParentID = row["CC_ParentID"].ToString();
                }
                if (row["CC_OfficialCode"] != null)
                {
                    model.CC_OfficialCode = row["CC_OfficialCode"].ToString();
                }
                if (row["CC_EqualCode"] != null)
                {
                    model.CC_EqualCode = row["CC_EqualCode"].ToString();
                }
                if (row["CC_SerialNumb"] != null && row["CC_SerialNumb"].ToString() != "")
                {
                    model.CC_SerialNumb = double.Parse(row["CC_SerialNumb"].ToString());
                }
                if (row["CC_Explain"] != null)
                {
                    model.CC_Explain = row["CC_Explain"].ToString();
                }
                if (row["isSave"] != null && row["isSave"].ToString() != "")
                {
                    model.isSave = int.Parse(row["isSave"].ToString());
                }
                if (row["CC_NodeType"] != null && row["CC_NodeType"].ToString() != "")
                {
                    model.CC_NodeType = int.Parse(row["CC_NodeType"].ToString());
                }
                if (row["CC_NodeSubType"] != null && row["CC_NodeSubType"].ToString() != "")
                {
                    model.CC_NodeSubType = int.Parse(row["CC_NodeSubType"].ToString());
                }
                if (row["isLeafNode"] != null && row["isLeafNode"].ToString() != "")
                {
                    model.isLeafNode = int.Parse(row["isLeafNode"].ToString());
                }
                if (row["AllParentId"] != null)
                {
                    model.AllParentId = row["AllParentId"].ToString();
                }
                if (row["HW_level"] != null && row["HW_level"].ToString() != "")
                {
                    model.HW_level = int.Parse(row["HW_level"].ToString());
                }
                if (row["CC_RegexInfoCode"] != null)
                {
                    model.CC_RegexInfoCode = row["CC_RegexInfoCode"].ToString();
                }
                if (row["AllParentName"] != null)
                {
                    model.AllParentName = row["AllParentName"].ToString();
                }
                if (row["CC_InferRuleTextByCode"] != null)
                {
                    model.CC_InferRuleTextByCode = row["CC_InferRuleTextByCode"].ToString();
                }
                if (row["ElementXML"] != null)
                {
                    if (model.CC_NodeType == 2)
                    {
                        model.ElementXML = Getxml("SYS_CaseCause\\ElementXML", model.CC_ID);
                    }
                    else
                    {
                        model.ElementXML = "";
                    }
                }
                if (row["OptimizeXML"] != null)
                {
                    model.OptimizeXML = row["OptimizeXML"].ToString();
                }
                if (row["AttrRelXML"] != null)
                {
                    model.AttrRelXML = row["AttrRelXML"].ToString();
                }
                if (row["ConfigXML"] != null)
                {
                    if (model.CC_NodeType == 2)
                    {
                        model.ConfigXML = Getxml("SYS_CaseCause\\ConfigXML", model.CC_ID);
                    }
                    else
                    {
                        model.ConfigXML = "";
                    }
                }
                if (row["XmlPath"] != null)
                {
                    model.XmlPath = row["XmlPath"].ToString();
                }
                if (row["CC_IsCombination"] != null && row["CC_IsCombination"].ToString() != "")
                {
                    model.CC_IsCombination = int.Parse(row["CC_IsCombination"].ToString());
                }
                if (row["CC_IsProcessNode"] != null && row["CC_IsProcessNode"].ToString() != "")
                {
                    model.CC_IsProcessNode = int.Parse(row["CC_IsProcessNode"].ToString());
                }
                if (row["CC_IsShowExpress"] != null && row["CC_IsShowExpress"].ToString() != "")
                {
                    model.CC_IsShowExpress = int.Parse(row["CC_IsShowExpress"].ToString());
                }
                if (row["CC_IsAgainProcessData"] != null && row["CC_IsAgainProcessData"].ToString() != "")
                {
                    model.CC_IsAgainProcessData = int.Parse(row["CC_IsAgainProcessData"].ToString());
                }
                if (row["CC_CommonCase"] != null && row["CC_CommonCase"].ToString() != "")
                {
                    model.CC_CommonCase = int.Parse(row["CC_CommonCase"].ToString());
                }
                if (row["CC_GlobalOrder"] != null)
                {
                    model.CC_GlobalOrder = row["CC_GlobalOrder"].ToString();
                }
                if (row["KeyWords"] != null)
                {
                    model.KeyWords = row["KeyWords"].ToString();
                }
                if (row["PrimaryKeyWords"] != null)
                {
                    model.PrimaryKeyWords = row["PrimaryKeyWords"].ToString();
                }
            }
            return model;
        }

        /// 返回xml
        /// </summary>
        /// <param name="moudleName"></param>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string Getxml(string moudleName, string ccID)
        {
            string webapiUrl1 = ConfigHelper.AppSettings("xmlPath") + "/system/config/v1/getfilecontent";
            RequestContent rc = new RequestContent() { systemName = ConfigHelper.AppSettings("getXml"), moudleName = moudleName, fileType = "xml", fileName = ccID };
            ComResult model = WebUtil.DoPost<ComResult>(webapiUrl1, rc, "UTF-8");
            if (model.result.code == "1")
            {
                return model.body.content;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 节点一级类型列表，如果ccid不为空，则获取该节点下的所有子节点的节点类型
        /// </summary>
        /// <param name="ccId">情形情节节点ID</param>
        /// <returns></returns>
        public DataTable GetCaseCauseType(string ccId = null)
        {
            StringBuilder strSql = new StringBuilder();
            if (string.IsNullOrEmpty(ccId))
            {
                strSql.Append(@"select CC_NodeType,CC_NodeTypeName from SYS_CaseCause_Type where CC_NodeParentID is null or CC_NodeParentID='' ");
            }
            else
            {
                strSql.Append(string.Format(@"  select CC_NodeType,case when CC_NodeParentID is null then CC_NodeTypeName+'(一级)'  when CC_NodeParentID is not null then CC_NodeTypeName+'(二级)' end as CC_NodeTypeName  from SYS_CaseCause_Type 
                              where CC_NodeType in 
                              ( 
                                  select distinct CC_NodeType from SYS_CaseCause where AllParentId like '%,{0},%' 
                                    union select distinct CC_NodeSubType from SYS_CaseCause where AllParentId like '%,{0},%'
                              ) order by CC_NodeType", ccId));
            }

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 添加来源界面批量类型下拉框选择类型（如果有一级节点类型下面的二级节点类型为空则显示一级节点类型否则只显示二级节点类型）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public DataTable GetCaseCauseTypeByCCId(string CC_ID)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_GetNodeTypeByCCId");

            oBase.AddInParameter(oCmd, "@CCId", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取所有节点类型
        /// </summary>
        /// <returns></returns>
        public DataTable GetCaseCauseTypeAll()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select CC_NodeType,CC_NodeTypeName from SYS_CaseCause_Type ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 根据父ID获取二级节点类型
        /// </summary>
        /// <param name="ccNodeType">节点类型ID</param>
        /// <returns></returns>
        public DataTable GetCaseCauseTypeByNodeType(string ccNodeType)
        {
            string sql = string.Format(@" select * from SYS_CaseCause_Type where CC_NodeParentID={0} ", ccNodeType);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable oT = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (oT != null && oT.Rows.Count > 0)
            {
                return oT;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获取二级节点类型（固定值）
        /// </summary>
        /// <param name="ccNodeType">节点类型ID</param>
        /// <returns></returns>
        public DataTable GetCaseCauseTypeByNodeType()
        {
            string sql = " select * from SYS_CaseCause_Type  where CC_NodeParentID is not null ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            DataTable oT = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (oT != null && oT.Rows.Count > 0)
            {
                return oT;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 相关节点（所有直系上级和下级）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public DataTable GetRelationStiuation(string CC_ID)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_GetAllDirectStiuation");

            oBase.AddInParameter(oCmd, "@Situation", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
        /// <summary>
        /// 相关节点（所有下级）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public DataTable GetAllBelowStiuation(string CC_ID)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_GetAllBelowStiuation");

            oBase.AddInParameter(oCmd, "@Situation", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public DataTable GetChildProcessStiuation1(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from dbo.SYS_CaseCause
            where CC_ParentID=@CC_ID and CC_IsProcessNode=1 and CC_IsCombination=1
            order by CC_SerialNumb
             ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public DataTable GetChildProcessStiuation(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"select * from dbo.SYS_CaseCause
            where CC_ParentID=@CC_ID and CC_IsProcessNode=1 
            order by CC_SerialNumb
             ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 修改顺序
        /// </summary>
        /// <param name="Previd"></param>
        /// <param name="Nestid"></param>
        /// <returns></returns>
        public bool UpCaseCause_SeralNum(string Previd, string Nestid)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_UpCaseCause_SeralNum");

            oBase.AddInParameter(oCmd, "@Previd", DbType.String, Previd);
            oBase.AddInParameter(oCmd, "@Nestid", DbType.String, Nestid);



            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        public bool UpCaseCause_IsAgainProcessData(string CC_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"update dbo.SYS_CaseCause set CC_IsAgainProcessData=0
            where  ','+AllParentId+',' like '%,'+@CC_ID+'%'  ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 删除
        /// 2017年5月22日 ljj修改
        /// 将SQL语句由delete from dbo.SYS_CaseCause where CC_ID=@CC_ID or (charindex(@CC_ID, AllParentId )>0)
        /// 改为delete from dbo.SYS_CaseCause where CC_ID='{0}' or (charindex(',{0},', ',' + AllParentId + ',')>0)"
        /// 修复删除时会误删其他无关节点的bug
        /// 并添加操作日志
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public bool DeleteSYS_CaseCause(string CC_ID, string CC_Name, string username)
        {
            try
            {
                string a = Other.GetIpAddress();
                string sqlWhere = String.Format(@"CC_ID='{0}' or (charindex(',{0},', ',' + AllParentId + ',')>0)",
                                                 CC_ID);
                string strSql = String.Format(@"delete  from dbo.SYS_CaseCause 
	                                         where {0}",
                                                 sqlWhere);
                if (OperLogDAL.AddOperLog(username, "delete", "SYS_CaseCause", "删除节点：" + CC_ID + "节点名称：" + CC_Name))
                {
                    Database oBase = DatabaseFactory.CreateDatabase(DbName);
                    DbCommand oCmd = oBase.GetSqlStringCommand(strSql);

                    //oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
                    if (oBase.ExecuteNonQuery(oCmd) > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("删除节点添加日志失败：" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 修改（推导规则）
        /// </summary>
        /// <param name="CC_InferRuleText"></param>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public bool Update_CC_InferRuleText(string CC_InferRuleTextByCode, string CC_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_CaseCause set ");
            strSql.Append(" CC_InferRuleTextByCode=@CC_InferRuleTextByCode");
            strSql.Append(" where CC_ID=@CC_ID ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            oBase.AddInParameter(oCmd, "@CC_InferRuleTextByCode", DbType.String, CC_InferRuleTextByCode);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 修改诉请要素与事实要素下的XMLPATH
        /// </summary>
        /// <param name="CC_InferRuleText"></param>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public void UpdateXmlPath(string CC_ID)
        {
            string sql = string.Format(@" exec sp_updateXmlPathByAnYouID '{0}' ", CC_ID);

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            oBase.ExecuteNonQuery(oCmd);
        }

        /// <summary>
        /// 修改刑事案由等其他案由下的XMLPATH
        /// </summary>
        /// <param name="CC_InferRuleText"></param>
        /// <param name="CC_ID"></param>
        /// <returns></returns>
        public void UpdateXmlPathByXS(string CC_ID)
        {
            string sql = string.Format(@" exec sp_updateXmlPathByXSAnYouID '{0}' ", CC_ID);

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            oBase.ExecuteNonQuery(oCmd);
        }

        /// <summary>
        /// 修改（节点信息）
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool Update(SYSCaseCauseInferCondition Model)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_CaseCause_InferCondition set ");
            strSql.Append("CC_ID=@CC_ID,");
            strSql.Append("IR_Name=@IR_Name,");

            strSql.Append("IR_Content=@IR_Content,");
            strSql.Append("IR_Remark=@IR_Remark");
            strSql.Append(" where IR_ID=@IR_ID ");

            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@IR_ID", DbType.String, Model.IR_ID);
            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Model.CC_ID);
            oBase.AddInParameter(oCmd, "@IR_Name", DbType.String, Model.IR_Name);

            oBase.AddInParameter(oCmd, "@IR_Content", DbType.String, Model.IR_Content);
            oBase.AddInParameter(oCmd, "@IR_Remark", DbType.String, Model.IR_Remark);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///  修改（XML）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="EI_ID"></param>
        /// <returns></returns>
        public bool UpdateElementXML(string CC_ID, string ElementXML)
        {

            RequestContent rc = new RequestContent() { systemName = ConfigHelper.AppSettings("getXml"), moudleName = "SYS_CaseCause\\ElementXML", fileType = "xml", fileName = CC_ID, content = ElementXML };
            string weburl = ConfigHelper.AppSettings("xmlPath") + "/system/config/v1/setfilecontent";
            ComResult model = WebUtil.DoPost<ComResult>(weburl, rc, "UTF-8");
            if (model.result.code == "1")
            {
                OperLogDAL.AddOperLog(PublicMethod.UserName, "操作xml", rc.fileName + "-" + rc.moudleName, rc.content);
                return true;
            }
            else if (model.result.code == "0")
            {
                return false;
            }
            else
            {
                return false;
            }
            //StringBuilder strSql = new StringBuilder();
            //strSql.Append("update SYS_CaseCause set ");
            //strSql.Append(" ElementXML=@ElementXML ");
            //strSql.Append(" where CC_ID=@CC_ID ");

            //Database oBase = DatabaseFactory.CreateDatabase(DbName);
            //DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            //oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            //oBase.AddInParameter(oCmd, "@ElementXML", DbType.String, ElementXML);
            //if (oBase.ExecuteNonQuery(oCmd) > 0)
            //{
            //    return true;
            //}
            //return false;
        }


        /// <summary>
        ///  修改（OptimizeXML）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="EI_ID"></param>
        /// <returns></returns>
        public bool UpdateOptimizeXML(string CC_ID, string OptimizeXML)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_CaseCause set ");
            strSql.Append(" OptimizeXML=@OptimizeXML ");
            strSql.Append(" where CC_ID=@CC_ID ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            oBase.AddInParameter(oCmd, "@OptimizeXML", DbType.String, OptimizeXML);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        ///  修改（AttrRelXML）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="EI_ID"></param>
        /// <returns></returns>
        public bool UpdateAttrRelXML(string CC_ID, string AttrRelXML)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_CaseCause set ");
            strSql.Append(" AttrRelXML=@AttrRelXML ");
            strSql.Append(" where CC_ID=@CC_ID ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            oBase.AddInParameter(oCmd, "@AttrRelXML", DbType.String, AttrRelXML);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        ///  修改（ConfigXML）
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="EI_ID"></param>
        /// <returns></returns>
        public bool UpdateConfigXML(string CC_ID, string ConfigXML)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update SYS_CaseCause set ");
            strSql.Append(" ConfigXML=@ConfigXML ");
            strSql.Append(" where CC_ID=@CC_ID ");

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
            oBase.AddInParameter(oCmd, "@ConfigXML", DbType.String, ConfigXML);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 修改（节点名称）
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateName(string CC_ID, string CC_Name)
        {
            if (!CC_Name.Contains("待删"))
            {
                Database oBase = DatabaseFactory.CreateDatabase(DbName);

                StringBuilder strSql = new StringBuilder();
                strSql.Append("update SYS_CaseCause set ");
                strSql.Append("CC_Name=@CC_Name");

                strSql.Append(" where CC_ID=@CC_ID ");

                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

                oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, CC_ID);
                oBase.AddInParameter(oCmd, "@CC_Name", DbType.String, CC_Name + "_待删");

                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
            }

            return false;
        }


        /// <summary>
        /// 查询根节点
        /// </summary>
        /// <param name="Cc_id"></param>
        /// <returns></returns>
        public DataTable Select_parentNode_Name(string Cc_id)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("  SELECT * FROM [SYS_CaseCause] where CC_ID = @Cc_id ");

            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Cc_id);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 根据节点获取他的子级节点
        /// </summary>
        /// <param name="Cc_id"></param>
        /// <returns></returns>
        public DataTable Select_parentNode_zi(string Cc_id)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);

            StringBuilder strSql = new StringBuilder();
            strSql.Append("  SELECT * FROM [SYS_CaseCause] where CC_ParentID = @CC_ID order by CC_SerialNumb");

            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@CC_ID", DbType.String, Cc_id);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return null;
            }

        }

        /// <summary>
        /// 判断是否是民事案由
        /// </summary>
        /// <param name="cc_Id"></param>
        /// <returns></returns>
        public bool IsCivilCase(string cc_Id)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" select COUNT(1) from SYS_CaseCause where CC_ID = @CC_ID and CHARINDEX(',1477,',','+AllParentId+',')>0  ");
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                db.AddInParameter(oCmd, "@CC_ID", DbType.String, cc_Id);
                Int32 i = Convert.ToInt32(db.ExecuteScalar(oCmd));
                if (i == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }


        /// <summary>
        /// 根据cc_Id获取争议焦点要素下的节点集合和争议焦点要素ID
        /// </summary>
        /// <param name="cc_Id"></param>
        /// <returns></returns>
        public DataSet GetDisputeElementData(string cc_Id)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" select  CC_ID,CC_Name,CC_NodeType,AllParentId from SYS_CaseCause where CHARINDEX(','+(select CC_ID from SYS_CaseCause where  CHARINDEX(','+@CC_ID+',',','+AllParentId+',')>0 and CC_Name='争议焦点要素')+',',','+AllParentId+',')>0 and CC_ID<>(select CC_ID from SYS_CaseCause where CHARINDEX(','+@CC_ID+',', ',' + AllParentId + ',') > 0 and CC_Name = '争议焦点要素'); select CC_ID from SYS_CaseCause where CHARINDEX(','+@CC_ID+',', ',' + AllParentId + ',') > 0 and CC_Name = '争议焦点要素'; ");
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                db.AddInParameter(oCmd, "@CC_ID", DbType.String, cc_Id);
                DataSet ds = db.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds;
                }
                else
                {
                    return new DataSet();
                }
            }
            catch (Exception ex)
            {
                return new DataSet();
            }
        }


        //获取该节点下诉请\事实\基本 要素下一级类型为“xml节点”的节点集合
        public DataTable GetElementData(string cc_Id, string elementType)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                StringBuilder strSql = new StringBuilder();
                strSql.Append(" select  CC_ID from SYS_CaseCause where CHARINDEX(','+(select CC_ID from SYS_CaseCause where  CHARINDEX(','+@CC_ID+',',','+AllParentId+',')>0 and CC_Name=@ElementType)+',',','+AllParentId+',')>0  and CC_NodeType in( 11,12 ) and CC_Name<>'属性' ");
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                db.AddInParameter(oCmd, "@CC_ID", DbType.String, cc_Id);
                db.AddInParameter(oCmd, "@ElementType", DbType.String, elementType);
                DataTable dt = db.ExecuteDataSet(oCmd).Tables[0];
                if (dt != null && dt.Rows.Count > 0)
                {
                    return dt;
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 修改二级节点类型事件（诉请要素18 事实要素19 基本要素20）
        /// </summary>
        /// <param name="cc_Ids">需要修改的节点集合</param>
        /// <param name="subNodeType">二级节点类型ID</param>
        /// <returns></returns>
        public bool UpdateNodeSubType(string cc_Ids, int subNodeType)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string strSql = string.Format(" update SYS_CaseCause set CC_NodeSubType={0} where CC_ID in({1}) ", subNodeType, cc_Ids);
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                int result = db.ExecuteNonQuery(oCmd);
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 判断是否是最后一级节点
        /// </summary>
        /// <param name="cc_Id"></param>
        /// <returns></returns>
        public bool IsLastNode(string cc_Id)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string strSql = string.Format(" select count(1) from SYS_CaseCause where CC_ParentID='{0}' ", cc_Id);
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                int result = Convert.ToInt32(db.ExecuteScalar(oCmd));
                if (result == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 修改一级节点类型事件（倒数第一级节点变为裁判结果（字典值为22） 倒数第二级节点变为裁判理由（字典值为17）倒数第三级节点变为争议焦点（字典值为16） 倒数第四级及以上节点变为争议焦点分类（字典值为15））
        /// </summary>
        /// <param name="cc_Ids">需要修改的节点集合</param>
        /// <param name="NodeType">一级节点类型ID</param>
        /// <returns></returns>
        public bool UpdateNodeType(string cc_Ids, int NodeType)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string strSql = string.Format(" update SYS_CaseCause set CC_NodeType={0} where CC_ID in({1}) ", NodeType, cc_Ids);
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                int result = db.ExecuteNonQuery(oCmd);
                if (result > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 更改节点排序
        /// </summary>
        /// <param name="sourceItemId"></param>
        /// <param name="targetItemId"></param>
        /// <returns></returns>
        public bool UpdateItemSort(SYS_CaseCause sourceItem, SYS_CaseCause targetItem, string moveState)
        {
            //操作节点添加日志
            OperLogDAL.AddOperLog(PublicMethod.UserName, "操作节点移动SYS_CaseCause", "操作节点移动：来源节点：" + sourceItem.CC_ID + "--" + sourceItem.CC_Name + " 目标节点:" + targetItem.CC_ID + "--" + targetItem.CC_Name + " 移动方式:" + moveState, "");
            double sourceSort = sourceItem.CC_SerialNumb;
            double targetSort = targetItem.CC_SerialNumb;
            string sql = "";
            //来源节点XML
            SYS_CaseCause caseCausexml = IsParentRealation1(sourceItem.CC_ID);
            caseCausexml.ElementXML = Getxml("SYS_CaseCause\\ElementXML", caseCausexml.CC_ID);

            //目标节点xml
            SYS_CaseCause caseTargetCausexml = IsParentRealation1(targetItem.CC_ID);
            caseTargetCausexml.ElementXML = Getxml("SYS_CaseCause\\ElementXML", caseTargetCausexml.CC_ID);

            //同一个父节点下的节点
            if (sourceItem.CC_ParentID == targetItem.CC_ParentID)
            {
                if (moveState == "上方" && sourceSort < targetSort)
                {
                    //修改节点移动后需要排序的节点
                    sql = string.Format(@" update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb-1 
                                    where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2}  ",
                                    sourceItem.CC_ParentID, sourceSort + 1, targetSort - 1);
                    //修改目标节点
                    sql = string.Format(@"{0};update SYS_CaseCause set CC_SerialNumb = {1}
                                      where CC_ID='{2}'",
                                   sql, targetItem.CC_SerialNumb - 1, sourceItem.CC_ID);
                }
                else if (moveState == "上方" && sourceSort > targetSort)
                {
                    sql = string.Format(@"update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb+1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2}",
                                      sourceItem.CC_ParentID, targetSort, sourceSort - 1);
                    //修改目标节点
                    sql = string.Format(@"{0};update SYS_CaseCause set CC_SerialNumb = {1}
                                      where CC_ID='{2}'",
                                   sql, targetItem.CC_SerialNumb, sourceItem.CC_ID);
                }
                else if (moveState == "下方" && sourceSort < targetSort)
                {
                    sql = string.Format(@"update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb-1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2}",
                                      sourceItem.CC_ParentID, sourceSort + 1, targetSort);
                    //修改目标节点
                    sql = string.Format(@"{0};update SYS_CaseCause set CC_SerialNumb = {1}
                                      where CC_ID='{2}'",
                                   sql, targetItem.CC_SerialNumb, sourceItem.CC_ID);
                }
                else if (moveState == "下方" && sourceSort > targetSort)
                {
                    sql = string.Format(@"update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb+1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2}",
                                      sourceItem.CC_ParentID, targetSort + 1, sourceSort - 1);
                    //修改目标节点
                    sql = string.Format(@"{0};update SYS_CaseCause set CC_SerialNumb = {1}
                                      where CC_ID='{2}'",
                                   sql, targetItem.CC_SerialNumb + 1, sourceItem.CC_ID);
                }
            }
            else
            {
                //获取当前节点的最后一个排号节点
                double SourceccSerialNumber = GetNodeChildSerialNumber(sourceItem.CC_ParentID, true);
                double targetccSerialNumber = GetNodeChildSerialNumber(targetItem.CC_ParentID, true);
                if (moveState == "上方")
                {
                    //目标节点排号排序
                    sql = string.Format(@"update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb+1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2};",
                                      targetItem.CC_ParentID, targetItem.CC_SerialNumb, targetccSerialNumber);
                    DataTable dt = treedal.NodeTree_Tree(sourceItem.CC_ParentID);
                    if (dt != null && dt.Rows.Count > 1)
                    {
                        //原节点排号排序
                        sql += string.Format(@"  update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb-1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2};",
                                    sourceItem.CC_ParentID, sourceItem.CC_SerialNumb + 1, SourceccSerialNumber);
                    }
                    string allParentID = targetItem.AllParentId.Replace(targetItem.CC_ID, "") + sourceItem.CC_ID;
                    string allParentName = targetItem.AllParentName.Replace(targetItem.CC_Name, "") + sourceItem.CC_Name;
                    //修改目标节点
                    sql += string.Format(@" update SYS_CaseCause set CC_SerialNumb = {0},CC_ParentID='{1}' where CC_ID='{2}';",
                                    targetItem.CC_SerialNumb, targetItem.CC_ParentID, sourceItem.CC_ID);

                    //原节点全部父级ID
                    string sourceAllParentId = sourceItem.AllParentId;
                    //目标节点全部父级ID
                    string targetAllParentId = targetItem.AllParentId.Replace("," + targetItem.CC_ID, "") + "," + sourceItem.CC_ID;

                    //原节点全部父级路径
                    string sourceAllParentName = sourceItem.AllParentName;
                    //目标节点全部父级路径
                    string targetAllParentName = targetItem.AllParentName.Replace("," + targetItem.CC_Name, "") + "," + sourceItem.CC_Name;

                    //原节点XMLPath
                    //string sourceXmlPath = sourceItem.XmlPath;

                    //目标节点XMLPath
                    //string targetXmlPath = targetItem.XmlPath.Replace("/" + targetItem.CC_Name, "") + (sourceItem.CC_Name == "属性" ? "" : "/" + sourceItem.CC_Name);


                    int i = targetAllParentId.Split(',').Length;

                    sql += string.Format(@" update SYS_CaseCause set HW_level='{0}'  where AllParentName like '{1}%' ;", i, sourceAllParentName);

                    sql += string.Format(@" update SYS_CaseCause set AllParentId=REPLACE(AllParentId,'{0}','{1}')  where AllParentId like '{0}%' ;", sourceAllParentId, targetAllParentId);

                    sql += string.Format(@" update SYS_CaseCause set AllParentName=REPLACE(AllParentName,'{0}','{1}')  where AllParentName like '{0}%' ;", sourceAllParentName, targetAllParentName);

                    //sql += string.Format(@" update SYS_CaseCause set XmlPath=REPLACE(XmlPath,'{0}','{1}')  where XmlPath like '{0}%' and CHARINDEX('{2}',AllParentId)>0 ;", sourceXmlPath, targetXmlPath, sourceItem.CC_ID);

                }
                else if (moveState == "下方")
                {
                    sql = string.Format(@"update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb+1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2};",
                                      targetItem.CC_ParentID, targetItem.CC_SerialNumb + 1, targetccSerialNumber);
                    DataTable dt = treedal.NodeTree_Tree(sourceItem.CC_ParentID);
                    if (dt != null && dt.Rows.Count > 1)
                    {
                        //原节点排号排序
                        sql += string.Format(@"  update SYS_CaseCause set CC_SerialNumb = CC_SerialNumb-1
                                      where CC_ParentID='{0}' and CC_SerialNumb between {1} and {2};",
                                        sourceItem.CC_ParentID, sourceItem.CC_SerialNumb + 1, SourceccSerialNumber);
                    }
                    string allParentID = targetItem.AllParentId.Replace(targetItem.CC_ID, "") + sourceItem.CC_ID;
                    string allParentName = targetItem.AllParentName.Replace(targetItem.CC_Name, "") + sourceItem.CC_Name;
                    //修改目标节点
                    sql += string.Format(@" update SYS_CaseCause set CC_SerialNumb = {0},CC_ParentID='{1}' where CC_ID='{2}';",
                                    targetItem.CC_SerialNumb + 1, targetItem.CC_ParentID, sourceItem.CC_ID);

                    //原节点全部父级ID
                    string sourceAllParentId = sourceItem.AllParentId;
                    //目标节点全部父级ID
                    string targetAllParentId = targetItem.AllParentId.Replace("," + targetItem.CC_ID, "") + "," + sourceItem.CC_ID;

                    //原节点全部父级路径
                    string sourceAllParentName = sourceItem.AllParentName;
                    //目标节点全部父级路径
                    string targetAllParentName = targetItem.AllParentName.Replace("," + targetItem.CC_Name, "") + "," + sourceItem.CC_Name;

                    //原节点XMLPath
                    //string sourceXmlPath = sourceItem.XmlPath;

                    ////目标节点XMLPath
                    //string targetXmlPath = targetItem.XmlPath.Replace("/" + targetItem.CC_Name, "") + (sourceItem.CC_Name == "属性" ? "" : "/" + sourceItem.CC_Name);


                    int i = targetAllParentId.Split(',').Length;

                    sql += string.Format(@" update SYS_CaseCause set HW_level='{0}'  where AllParentName like '{1}%' ;", i, sourceAllParentName);

                    sql += string.Format(@" update SYS_CaseCause set AllParentId=REPLACE(AllParentId,'{0}','{1}')  where AllParentId like '{0}%' ;", sourceAllParentId, targetAllParentId);

                    sql += string.Format(@" update SYS_CaseCause set AllParentName=REPLACE(AllParentName,'{0}','{1}')  where AllParentName like '{0}%' ;", sourceAllParentName, targetAllParentName);

                    //sql += string.Format(@" update SYS_CaseCause set XmlPath=REPLACE(XmlPath,'{0}','{1}')  where XmlPath like '{0}%'  and CHARINDEX('{2}',AllParentId)>0  ;", sourceXmlPath, targetXmlPath, sourceItem.CC_ID);

                }
            }
            if (moveState == "下级")
            {
                //原节点全部父级ID
                string sourceAllParentId = sourceItem.AllParentId;
                //目标节点全部父级ID
                string targetAllParentId = targetItem.AllParentId + "," + sourceItem.CC_ID;

                //原节点全部父级路径
                string sourceAllParentName = sourceItem.AllParentName;
                //目标节点全部父级路径
                string targetAllParentName = targetItem.AllParentName + "," + sourceItem.CC_Name;

                //原节点XMLPath
                //string sourceXmlPath = sourceItem.XmlPath;

                ////目标节点XMLPath
                //string targetXmlPath = targetItem.XmlPath + (sourceItem.CC_Name == "属性" ? "" : "/" + sourceItem.CC_Name);

                int i = targetAllParentId.Split(',').Length;

                sql += string.Format(@" update SYS_CaseCause set HW_level='{0}'  where AllParentName like '{1}%' ;", i, sourceAllParentName);

                sql += string.Format(@" update SYS_CaseCause set AllParentId=REPLACE(AllParentId,'{0}','{1}')  where AllParentId like '{0}%' ;", sourceAllParentId, targetAllParentId);

                sql += string.Format(@" update SYS_CaseCause set AllParentName=REPLACE(AllParentName,'{0}','{1}')  where AllParentName like '{0}%' ;", sourceAllParentName, targetAllParentName);

                //sql += string.Format(@" update SYS_CaseCause set XmlPath=REPLACE(XmlPath,'{0}','{1}')  where XmlPath like '{0}%'  and CHARINDEX('{2}',AllParentId)>0 ;", sourceXmlPath, targetXmlPath, sourceItem.CC_ID);

                double ccSerialNumber = GetNodeChildSerialNumber(targetItem.CC_ID);
                sql += string.Format(@" update  SYS_CaseCause set CC_SerialNumb ='{0}', CC_ParentID='{1}' where CC_ID='{2}' ;", ccSerialNumber, targetItem.CC_ID, sourceItem.CC_ID);

                //最后一个节点的排号
                double lastSerialNumber = GetNodeChildSerialNumber(sourceItem.CC_ParentID, true);
                sql += string.Format(@" update SYS_CaseCause set CC_SerialNumb =CC_SerialNumb-1 where CC_SerialNumb between {0} and {1} and  CC_ParentID='{2}' ;", sourceSort + 1, lastSerialNumber, sourceItem.CC_ParentID);



                #region MyRegion
                ////获取来源节点下面的子节点集合
                //DataTable dt = treedal.NodeTree_Tree(sourceItem.CC_ID);
                //if (dt != null && dt.Rows.Count > 0)
                //{
                //    for (int i = 0; i < dt.Rows.Count; i++)
                //    {
                //        SYS_CaseCause obj = GetCaseCauseByCcid(dt.Rows[i]["id"].ToString());
                //        string nodeStr = obj.AllParentId;
                //        int index = nodeStr.IndexOf("," + sourceItem.CC_ID);
                //        string resAllParentId = nodeStr.Insert(index, "," + targetItem.CC_ID);
                //        nodeStr = obj.AllParentName;
                //        index = nodeStr.IndexOf("," + sourceItem.CC_Name);
                //        string resAllParentName = nodeStr.Insert(index, "," + targetItem.CC_Name);
                //        sql += string.Format(@" update  SYS_CaseCause set AllParentId='{0}',AllParentName='{1}' where CC_ID='{2}'  ", resAllParentId, resAllParentName, obj.CC_ID);

                //    }
                //    //原节点全部父级ID
                //    string sourceAllParentId = sourceItem.AllParentId;
                //    //目标节点全部父级ID
                //    string targetAllParentId = targetItem.AllParentId + "," + sourceItem.CC_ID;

                //    //原节点全部父级路径
                //    string sourceAllParentName = sourceItem.AllParentName;
                //    //目标节点全部父级路径
                //    string targetAllParentName = targetItem.AllParentName + "," + sourceItem.CC_Name;

                //    sql += string.Format(@" update SYS_CaseCause set AllParentId=REPLACE(AllParentId,'{0}','{1}')  where AllParentId like '{0}%' ",sourceAllParentId,targetAllParentId);

                //    sql += string.Format(@" update SYS_CaseCause set AllParentName=REPLACE(AllParentName,'{0}','{1}')  where AllParentName like '{0}%' ", sourceAllParentName, targetAllParentName);

                //    double ccSerialNumber = GetNodeChildSerialNumber(targetItem.CC_ID);
                //    sql += string.Format(@" update  SYS_CaseCause set CC_SerialNumb ='{0}', CC_ParentID='{1}' where CC_ID='{2}' ", ccSerialNumber, targetItem.CC_ID, sourceItem.CC_ID);

                //    //最后一个节点的排号
                //    double lastSerialNumber = GetNodeChildSerialNumber(sourceItem.CC_ParentID, true);
                //    sql += string.Format(@" update SYS_CaseCause set CC_SerialNumb =CC_SerialNumb-1 where CC_SerialNumb between {0} and {1} and  CC_ParentID='{2}' ", sourceSort + 1, lastSerialNumber, sourceItem.CC_ParentID);


                //}
                //else //没有子节点
                //{
                //    string str = sourceItem.AllParentId;
                //    int index = str.IndexOf("," + sourceItem.CC_ID);
                //    string resAllParentId = str.Insert(index, "," + targetItem.CC_ID);
                //    str = sourceItem.AllParentName;
                //    index = str.IndexOf("," + sourceItem.CC_Name);
                //    string resAllParentName = str.Insert(index, "," + targetItem.CC_Name);
                //    double ccSerialNumber = GetNodeChildSerialNumber(targetItem.CC_ID);
                //    sql = string.Format(@" update  SYS_CaseCause set CC_SerialNumb ='{0}',
                //                               CC_ParentID='{1}',AllParentId='{2}',AllParentName='{3}' where CC_ID='{4}' ;", ccSerialNumber, targetItem.CC_ID, resAllParentId, resAllParentName, sourceItem.CC_ID);
                //}
                ////最后一个节点的排号
                //double lastSerialNumber = GetNodeChildSerialNumber(sourceItem.CC_ParentID, true);
                //sql += string.Format(@" update SYS_CaseCause set CC_SerialNumb =CC_SerialNumb-1 where CC_SerialNumb between {0} and {1} and  CC_ParentID='{2}' ", sourceSort + 1, lastSerialNumber, sourceItem.CC_ParentID); 
                #endregion
            }

            int result = 0;
            if (!string.IsNullOrEmpty(sql.ToString()))
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
                result = db.ExecuteNonQuery(oCmd);

            }

            //节点移动
            if ((!string.IsNullOrEmpty(caseCausexml.ElementXML) || !string.IsNullOrEmpty(caseTargetCausexml.ElementXML)) && sourceItem.CC_NodeType == 11 && targetItem.CC_NodeType == 11)
            {
                XmlDocument doc = new XmlDocument();
                XmlDocument doc1 = new XmlDocument();
                XmlNode objxml = null;
                XmlNode objParentXml = null;
                XmlNode objxml1 = null;
                XmlNode objParentXml1 = null;
                if (!string.IsNullOrEmpty(caseCausexml.ElementXML))
                {
                    string xml = caseCausexml.ElementXML.Replace("\r", "").Replace("\t", "").Replace("\n", "").Replace("-", "").Replace("、", @"");
                    doc.LoadXml(xml);

                    //int i = sourceItem.AllParentName.LastIndexOf(caseCausexml.CC_Name);
                    //string xmlPath = sourceItem.AllParentName.Substring(i);
                    //string[] strs = xmlPath.Split(',');
                    //StringBuilder sb = new StringBuilder();
                    //foreach (var item in strs)
                    //{
                    //    sb.Append("/" + item);
                    //}
                    //原节点路由
                    string res1 = sourceItem.XmlPath.ToString();
                    if (!string.IsNullOrEmpty(res1))
                    {
                        objxml = doc.SelectSingleNode(res1);
                        if (objxml != null)
                        {
                            objParentXml = objxml.ParentNode;
                        }
                    }
                }
                bool b = false;
                if (!string.IsNullOrEmpty(caseTargetCausexml.ElementXML))
                {
                    if (caseCausexml.CC_ID == caseTargetCausexml.CC_ID)
                    {
                        doc1 = doc;
                        b = true;
                    }
                    else
                    {
                        string xml = caseTargetCausexml.ElementXML.Replace("\r", "").Replace("\t", "").Replace("\n", "").Replace("-", "").Replace("、", @"");
                        doc1.LoadXml(xml);
                    }

                    //int i = targetItem.AllParentName.LastIndexOf(caseTargetCausexml.CC_Name);
                    //string xmlPath = targetItem.AllParentName.Substring(i);
                    //string[] strs = xmlPath.Split(',');
                    //StringBuilder sb = new StringBuilder();
                    //foreach (var item in strs)
                    //{
                    //    sb.Append("/" + item);
                    //}
                    //目标节点路由
                    string res2 = targetItem.XmlPath.ToString();
                    if (!string.IsNullOrEmpty(res2))
                    {
                        objxml1 = doc1.SelectSingleNode(res2);
                        if (objxml1 != null)
                        {
                            objParentXml1 = objxml1.ParentNode;
                        }
                    }
                }

                //XmlElement objxml001 = (XmlElement)objxml1;
                //XmlAttribute objxml01 = objxml001.GetAttributeNode(objxml.Attributes[0].Name);

                //XmlNode objParenetXml = objxml.ParentNode;          
                //XmlAttributeCollection objcola1 = objxml.Attributes;
                //XmlAttributeCollection objcola2 = objxml1.Attributes;
                if (moveState == "上方")
                {
                    if (objParentXml != null)
                    {
                        objParentXml.RemoveChild(objxml);
                    }
                    if (objParentXml1 != null && objxml != null && objxml1 != null)
                    {
                        objParentXml1.InsertBefore(doc1.ImportNode(objxml, true), objxml1);
                    }
                    else if (objParentXml1 != null && objxml1 != null)
                    {
                        XmlElement objElement = doc1.CreateElement(sourceItem.CC_Name);
                        objParentXml1.AppendChild(objElement);
                        // objParentXml1.AppendChild(doc1.ImportNode(objElement,true));
                    }
                }
                else if (moveState == "下方")
                {
                    if (objParentXml != null)
                    {
                        objParentXml.RemoveChild(objxml);
                    }
                    if (objParentXml1 != null && objxml != null && objxml1 != null)
                    {
                        objParentXml1.InsertAfter(doc1.ImportNode(objxml, true), objxml1);
                    }
                    else if (objParentXml1 != null && objxml1 != null)
                    {
                        XmlElement objElement = doc1.CreateElement(sourceItem.CC_Name);
                        objParentXml1.AppendChild(objElement);
                        // objParentXml1.AppendChild(doc1.ImportNode(objElement,true));
                    }
                }
                else if (moveState == "下级")
                {
                    if (objParentXml != null)
                    {
                        objParentXml.RemoveChild(objxml);
                    }
                    if (objParentXml1 != null && objxml != null && objxml1 != null)
                    {
                        objxml1.AppendChild(objxml);
                    }
                    else if (objParentXml1 != null && objxml1 != null)
                    {
                        XmlElement objElement = doc1.CreateElement(sourceItem.CC_Name);
                        objParentXml1.AppendChild(objElement);
                        // objxml1.AppendChild(doc1.ImportNode(objElement, true));
                    }
                }
                if (b)
                {
                    if (!string.IsNullOrEmpty(doc1.InnerXml.ToString()))
                    {
                        string res3 = FormatXML(doc1.InnerXml.ToString());
                        bool res = UpdateElementXML(caseCausexml.CC_ID, res3);
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(doc.InnerXml.ToString()))
                    {
                        string res3 = FormatXML(doc.InnerXml.ToString());
                        bool res = UpdateElementXML(caseCausexml.CC_ID, res3);
                    }

                    if (!string.IsNullOrEmpty(doc1.InnerXml.ToString()))
                    {
                        string res3 = FormatXML(doc1.InnerXml.ToString());
                        bool res = UpdateElementXML(caseTargetCausexml.CC_ID, res3);
                    }
                }
            }
            else if (!string.IsNullOrEmpty(caseCausexml.ElementXML) && sourceItem.CC_NodeType == 12 && targetItem.CC_NodeType == 12 && sourceItem.CC_ParentID == targetItem.CC_ParentID)
            {
                XmlDocument doc = new XmlDocument();
                XmlNode objxml = null;
                string xml = caseCausexml.ElementXML.Replace("\r", "").Replace("\t", "").Replace("\n", "").Replace("-", "").Replace("、", @"");
                doc.LoadXml(xml);

                int i = sourceItem.AllParentName.LastIndexOf(caseCausexml.CC_Name);
                string xmlPath = sourceItem.AllParentName.Substring(i);
                int j = xmlPath.LastIndexOf(",属性");
                xmlPath = xmlPath.Substring(0, j);
                string[] strs = xmlPath.Split(',');
                StringBuilder sb = new StringBuilder();
                foreach (var item in strs)
                {
                    sb.Append("/" + item);
                }
                //原节点路由
                string res1 = sb.ToString();
                if (!string.IsNullOrEmpty(res1))
                {
                    XmlElement obj = null;
                    objxml = doc.SelectSingleNode(res1);
                    if (objxml != null)
                    {
                        obj = (XmlElement)objxml;
                        obj.RemoveAllAttributes();
                    }
                    DataTable dt = GetAttributesByCCID(sourceItem.CC_ParentID);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        foreach (DataRow item in dt.Rows)
                        {
                            string ss = item["CC_Name"].ToString();
                            obj.SetAttribute(item[0].ToString(), "");
                        }
                    }
                    if (!string.IsNullOrEmpty(doc.InnerXml.ToString()))
                    {
                        string res3 = FormatXML(doc.InnerXml.ToString());
                        bool res = UpdateElementXML(caseCausexml.CC_ID, res3);
                    }
                }
            }
            else if ((!string.IsNullOrEmpty(caseCausexml.ElementXML) || !string.IsNullOrEmpty(caseTargetCausexml.ElementXML)) && (sourceItem.CC_NodeType == 11 || sourceItem.CC_NodeType == 12))
            {
                if (sourceItem.CC_NodeType == 12)
                {
                    //来源节点是属性节点，属性节点不移动
                    return true;
                }
                else
                {
                    XmlDocument doc = new XmlDocument();
                    XmlDocument doc1 = new XmlDocument();
                    XmlNode objxml = null;
                    XmlNode objParentXml = null;
                    XmlNode objxml1 = null;
                    XmlNode objParentXml1 = null;
                    if (!string.IsNullOrEmpty(caseCausexml.ElementXML))
                    {
                        string xml = caseCausexml.ElementXML.Replace("\r", "").Replace("\t", "").Replace("\n", "").Replace("-", "").Replace("、", @"");
                        doc.LoadXml(xml);

                        //int i = sourceItem.AllParentName.LastIndexOf(caseCausexml.CC_Name);
                        //string xmlPath = sourceItem.AllParentName.Substring(i);
                        //string[] strs = xmlPath.Split(',');
                        //StringBuilder sb = new StringBuilder();
                        //foreach (var item in strs)
                        //{
                        //    sb.Append("/" + item);
                        //}
                        //原节点路由
                        string res1 = sourceItem.XmlPath.ToString();
                        if (!string.IsNullOrEmpty(res1))
                        {
                            objxml = doc.SelectSingleNode(res1);
                            if (objxml != null)
                            {
                                objParentXml = objxml.ParentNode;
                            }
                        }
                    }
                    bool b = false;
                    if (!string.IsNullOrEmpty(caseTargetCausexml.ElementXML))
                    {
                        if (caseCausexml.CC_ID == caseTargetCausexml.CC_ID)
                        {
                            doc1 = doc;
                            b = true;
                        }
                        else
                        {
                            string xml = caseTargetCausexml.ElementXML.Replace("\r", "").Replace("\t", "").Replace("\n", "").Replace("-", "").Replace("、", @"");
                            doc1.LoadXml(xml);
                        }

                        //int i = targetItem.AllParentName.LastIndexOf(caseTargetCausexml.CC_Name);
                        //string xmlPath = targetItem.AllParentName.Substring(i);
                        //string[] strs = xmlPath.Split(',');
                        //StringBuilder sb = new StringBuilder();
                        //foreach (var item in strs)
                        //{
                        //    sb.Append("/" + item);
                        //}
                        //目标节点路由
                        string res2 = targetItem.XmlPath.ToString();
                        if (!string.IsNullOrEmpty(res2))
                        {
                            //属性节点xpath的当前节点和父节点是一个
                            if (targetItem.CC_NodeType == 12)
                            {
                                objParentXml1 = doc1.SelectSingleNode(res2);
                            }
                            else
                            {
                                objxml1 = doc1.SelectSingleNode(res2);
                                if (objxml1 != null)
                                {
                                    objParentXml1 = objxml1.ParentNode;
                                }
                            }
                        }
                    }

                    //XmlElement objxml001 = (XmlElement)objxml1;
                    //XmlAttribute objxml01 = objxml001.GetAttributeNode(objxml.Attributes[0].Name);

                    //XmlNode objParenetXml = objxml.ParentNode;          
                    //XmlAttributeCollection objcola1 = objxml.Attributes;
                    //XmlAttributeCollection objcola2 = objxml1.Attributes;
                    if (moveState == "上方")
                    {
                        if (objParentXml != null)
                        {
                            objParentXml.RemoveChild(objxml);
                        }
                        if (objParentXml1 != null && objxml != null)
                        {
                            //属性节点判断属性后有节点则插入下一个节点的上方 否则直接添加到父节点的下方
                            if (targetItem.CC_NodeType != 12)
                            {
                                objParentXml1.InsertBefore(doc1.ImportNode(objxml, true), objxml1);
                            }
                            else
                            {
                                if (objParentXml1.FirstChild != null)
                                {
                                    objParentXml1.InsertBefore(doc1.ImportNode(objxml, true), objParentXml1.FirstChild);
                                }
                                else
                                {
                                    objParentXml1.AppendChild(doc1.ImportNode(objxml, true));
                                }
                            }
                        }
                        else if (objParentXml1 != null && objxml1 != null)
                        {
                            XmlElement objElement = doc1.CreateElement(sourceItem.CC_Name);
                            objParentXml1.AppendChild(objElement);
                            // objParentXml1.AppendChild(doc1.ImportNode(objElement,true));
                        }
                    }
                    else if (moveState == "下方")
                    {
                        if (objParentXml != null)
                        {
                            objParentXml.RemoveChild(objxml);
                        }
                        if (objParentXml1 != null && objxml != null)
                        {
                            if (targetItem.CC_NodeType != 12)
                            {
                                objParentXml1.InsertAfter(doc1.ImportNode(objxml, true), objxml1);
                            }
                            else
                            {
                                if (objParentXml1.FirstChild != null)
                                {
                                    objParentXml1.InsertBefore(doc1.ImportNode(objxml, true), objParentXml1.FirstChild);
                                }
                                else
                                {
                                    objParentXml1.AppendChild(doc1.ImportNode(objxml, true));
                                }
                            }
                        }
                        else if (objParentXml1 != null && objxml1 != null)
                        {
                            XmlElement objElement = doc1.CreateElement(sourceItem.CC_Name);
                            objParentXml1.AppendChild(objElement);
                            // objParentXml1.AppendChild(doc1.ImportNode(objElement,true));
                        }
                    }
                    else if (moveState == "下级")
                    {
                        if (objParentXml != null)
                        {
                            objParentXml.RemoveChild(objxml);
                        }
                        if (objParentXml1 != null && objxml != null)
                        {
                            objxml1.AppendChild(objxml);
                        }
                        else if (objParentXml1 != null && objxml1 != null)
                        {
                            XmlElement objElement = doc1.CreateElement(sourceItem.CC_Name);
                            objParentXml1.AppendChild(objElement);
                            // objxml1.AppendChild(doc1.ImportNode(objElement, true));
                        }
                    }
                    if (b)
                    {
                        if (!string.IsNullOrEmpty(doc1.InnerXml.ToString()))
                        {
                            string res3 = FormatXML(doc1.InnerXml.ToString());
                            bool res = UpdateElementXML(caseCausexml.CC_ID, res3);
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(doc.InnerXml.ToString()))
                        {
                            string res3 = FormatXML(doc.InnerXml.ToString());
                            bool res = UpdateElementXML(caseCausexml.CC_ID, res3);
                        }

                        if (!string.IsNullOrEmpty(doc1.InnerXml.ToString()))
                        {
                            string res3 = FormatXML(doc1.InnerXml.ToString());
                            bool res = UpdateElementXML(caseTargetCausexml.CC_ID, res3);
                        }
                    }
                }
            }
            return result > 0 ? true : false;
        }


        /// <summary>
        /// 获取节点下面子节点的排序序号
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public double GetNodeChildSerialNumber(string cc_ID, bool NeedLast = false)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string sql = string.Format(@" 	select MAX(CC_SerialNumb) from SYS_CaseCause where CC_ParentID='{0}' ", cc_ID);
                DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
                string obj = db.ExecuteScalar(oCmd).ToString();
                if (NeedLast == true)
                {
                    return string.IsNullOrEmpty(obj) ? 1 : double.Parse(obj.ToString());
                }
                else
                {
                    return string.IsNullOrEmpty(obj) ? 1 : double.Parse(obj.ToString()) + 1;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        ///判断目标节点是否是原节点的子集 
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public bool IsParentRealation(string source_ID, string target_ID)
        {
            string[] strs = new string[0];
            DataTable dt = new DataTable();
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" 	select AllParentId from SYS_CaseCause where CC_ID='{0}' ", target_ID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null)
            {
                int index = obj.ToString().LastIndexOf(',');
                if (index != -1)
                {
                    strs = obj.ToString().Remove(index).Split(',');
                    foreach (var str in strs)
                    {
                        if (str == source_ID)
                        {
                            return true;
                        }
                    }
                }
            }
            else
            {
                return false;
            }
            return false;

            //Database db1 = DatabaseFactory.CreateDatabase(DbName);
            //string sql1 = string.Format(@" select CC_ID from SYS_CaseCause where CC_NodeType=2 ");
            //DbCommand oCmd1 = db.GetSqlStringCommand(sql1.ToString());
            //var obj1 = db1.ExecuteDataSet(oCmd1);
            //if (obj1 != null && obj1.Tables[0] != null && obj1.Tables[0].Rows.Count > 0)
            //{
            //        dt = obj1.Tables[0];
            //}
            //if (strs.Length > 0 && dt.Rows.Count > 0)
            //{
            //    for (int i = strs.Length-1; i >= 0; i--)
            //    {
            //        if (dt.Select("CC_ID='" + strs[i]+"'").Length > 0)
            //        {
            //            res= strs[i];
            //            break;
            //        }
            //    }
            //}                 
        }

        /// <summary>
        ///获取一个节点的父级的第一个案由节点
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public SYS_CaseCause IsParentRealation1(string ccID)
        {
            SYS_CaseCause objCaseCause = new SYS_CaseCause();
            string[] strs = new string[0];
            DataTable dt = new DataTable();

            string sql = string.Format(@" 	select AllParentId from SYS_CaseCause where CC_ID='{0}' ", ccID);
            string obj = SqlManage.ExecuteScalarSql(sql, DbName);
            strs = obj.ToString().Split(',');

            string sql1 = (" select CC_ID,CC_Name from SYS_CaseCause where CC_NodeType=2 ");
            dt = SqlManage.GetDataTableBySqlCondition(sql1, DbName);
            //string sql1 =(" select CC_ID,CC_Name from SYS_CaseCause where CC_NodeType=")+(ConfigHelper.AppSettings("Version") =="160"? "2 " : "28 ");

            if (strs.Length > 0 && dt.Rows.Count > 0)
            {
                for (int i = strs.Length - 1; i >= 0; i--)
                {
                    if (dt.Select("CC_ID='" + strs[i] + "'").Length > 0)
                    {
                        objCaseCause = GetCaseCauseByCcid(strs[i].ToString());
                        break;
                    }
                }
            }
            return objCaseCause;
        }


        /// <summary>
        /// 是否是最后一级案由
        /// </summary>
        /// <param name="cc_ID"></param>
        /// <returns></returns>
        public bool IsLastAnYou(string cc_ID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" 	select COUNT(1) from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_NodeType=2 and CC_ID<>'{0}' ", cc_ID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && Convert.ToInt32(obj) > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 将字符串转换为XML格式字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public string FormatXML(string str)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(str);
            StringBuilder strbu = new StringBuilder();
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(strbu);
            System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter(stringWriter);
            xmlWriter.Formatting = System.Xml.Formatting.Indented;
            doc.WriteTo(xmlWriter);
            return strbu.ToString();
        }


        /// <summary>
        /// 根据ccid获取该节点下面的属性
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public DataTable GetAttributesByCCID(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" 	select CC_Name from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_NodeType=12 and CC_ID<>'{0}' order by CC_SerialNumb asc ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteDataSet(oCmd);
            if (obj != null && obj.Tables.Count > 0)
            {
                return obj.Tables[0];
            }
            return new DataTable();
        }

        /// <summary>
        /// 根据ccid判断该节点下是否有属性xml节点
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string IsHaveProperty(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select CC_ID from SYS_CaseCause where CC_ParentID='{0}' and CC_Name='属性' ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 添加属性后更改排序
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public void AddPropertyUpdateSerialNum(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" update SYS_CaseCause set CC_SerialNumb=CC_SerialNumb+1 where CC_ParentID='{0}' ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            Int32 obj = db.ExecuteNonQuery(oCmd);
        }

        /// <summary>
        /// 根据CCID获取下级节点
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public List<string> GetTongJiNodes(string ccID)
        {
            List<string> obj = new List<string>();
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select CC_Name from [dbo].[SYS_CaseCause] where CC_ParentID='{0}' ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in table.Tables[0].Rows)
                {
                    obj.Add(Convert.ToString(item[0]));
                }
            }
            return obj;
        }

        /// <summary>
        /// 根据CCID获取下级包含事实要素，基本要素，诉请要素的CCID
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string[] GetElementCcid(string ccID)
        {
            List<string> str = new List<string>();
            string result = string.Empty;
            string obj1 = string.Empty;
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select AllParentId from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_Name in('基本要素','事实要素','诉请要素');select CC_Name from SYS_CaseCause where CC_ID='{0}';select CC_ID from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_Name in('基本要素','事实要素','诉请要素');  ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                str.Clear();
                foreach (DataRow item in table.Tables[0].Rows)
                {
                    string[] obj = item[0].ToString().Split(',');
                    if (obj.Length > 0)
                    {
                        foreach (string res in obj)
                        {
                            if (!str.Contains(res))
                            {
                                str.Add(res);
                            }
                        }
                    }
                }

                foreach (DataRow item in table.Tables[2].Rows)
                {
                    obj1 += "," + item[0].ToString();
                }

                foreach (var item in str)
                {
                    result += ",'" + item + "'";
                }
                string[] str1 = { result.Remove(0, 1).ToString(), table.Tables[1].Rows[0][0].ToString(), obj1.ToString() }; ;
                return str1;
            }
            else
            {
                return new string[] { };
            }
        }


        /// <summary>
        /// 得到节点的下级
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable GetNodeTree(string ccID, string other)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select  CC_ID as id,CC_Name as name from dbo.SYS_CaseCause where CC_ParentID= '{0}' and CC_ID in({1}) ", ccID, other);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 得到节点的下级
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable GetNodeTree(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select  CC_ID ,CC_Name  from dbo.SYS_CaseCause where CC_ParentID= '{0}'  ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据CCID获取该节点下是否配置了来源规则
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public bool GetExpressSourceByCCId(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select COUNT(1) from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_ID in(select distinct CC_ID from SYS_Express_Source) ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return Convert.ToInt32(obj) > 0 ? true : false;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据CCID删除该节点
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public bool DeleteCaseCauseByCCId(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" delete from SYS_CaseCause where CC_ID='{0}'  ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            int obj = db.ExecuteNonQuery(oCmd);
            if (obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到刑事数据处理节点的下级节点
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable GetCriminalNodes(string ccID)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string sql = string.Format(@" select CC_ID,CC_Name,CC_ParentID,AllParentId,CC_GlobalOrder,CC_NodeType,CC_NodeSubType,CC_SerialNumb from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0 ", ccID);
                DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
                DataSet table = db.ExecuteDataSet(oCmd);
                if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
                {
                    return table.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 得到刑事数据处理节点的下级节点,民事跑数据智能数据部用
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable GetCriminalNodesByData(string ccID)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string sql = string.Format(@" select CC_ID,CC_Name,CC_ParentID,AllParentId,CC_GlobalOrder,CC_NodeType,CC_NodeSubType,CC_SerialNumb from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0 and CC_Name<>'属性'  and CC_ID not in (select CC_ID  from SYS_Element_Ruleword_Rel where lock=1) ", ccID);
                DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
                DataSet table = db.ExecuteDataSet(oCmd);
                if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
                {
                    return table.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }



        /// <summary>
        /// 得到刑事数据处理节点的下级节点
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public DataTable GetCriminalNodesNew(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select CC_ID,CC_Name,CC_ParentID,AllParentId,CC_GlobalOrder,CC_NodeType,CC_NodeSubType from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0  and CC_ID not in(select distinct CC_ID from SYS_Express_Source) ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据CCID修改该节点XmlPath
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public bool UpdateXmlPathByCCId(string xmlPath, string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" update SYS_CaseCause set XmlPath='{0}' where CC_ID='{1}'  ", xmlPath, ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            int obj = db.ExecuteNonQuery(oCmd);
            if (obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据xml中xpath获取父级和同级的xPath
        /// </summary>
        /// <param name="xPath"></param>
        /// <returns></returns>
        public DataSet GetXpath(string xPath)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select XmlPath from SYS_CaseCause where cc_id=(select cc_parentid from SYS_CaseCause where XmlPath='{0}');select XmlPath from SYS_CaseCause where cc_parentid=(select cc_parentid from SYS_CaseCause where XmlPath='{0}')  and cc_id<>(select cc_id from SYS_CaseCause where XmlPath='{0}') ", xPath);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet ds = db.ExecuteDataSet(oCmd);
            return ds;
        }

        /// <summary>
        /// 获取导出数据类型下的数据
        /// </summary>
        /// <param name="ccId">节点ID</param>
        /// <param name="caseCauseType">类别</param>
        /// <returns></returns>
        public DataTable GetDaoChuData(string ccId, string caseCauseType)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@"select * 
from SYS_CaseCause  
where CHARINDEX(',{0},',','+AllParentId+',')>0 
and CC_NodeType in(select CC_NodeType from dbo.SYS_CaseCause_Type where CC_NodeTypeName in('{1}'))
order by CC_NodeType asc", ccId, caseCauseType);

            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取导出第二数据类型下的数据
        /// </summary>
        /// <param name="ccId">节点ID</param>
        /// <param name="caseCauseType">类别</param>
        /// <returns></returns>
        public DataTable GetDaoChuSecondData(string ccId, string caseCauseType)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@"select * 
from SYS_CaseCause  
where CHARINDEX(',{0},',','+AllParentId+',')>0 
and CC_NodeSubType in(select CC_NodeType from dbo.SYS_CaseCause_Type where CC_NodeTypeName in('{1}'))
order by CC_NodeType asc", ccId, caseCauseType);

            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取正则编号
        /// </summary>
        /// <param name="ccName"></param>
        /// <param name="ccNodeTypeName"></param>
        /// <param name="xmlPath"></param>
        /// <param name="AMName"></param>
        /// <returns></returns>
        public string GetRegexInfoCode(string ccName, string ccNodeTypeName, string xmlPath, string AMName, string clickNodeTag)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@"select top 1 CC_RegexInfoCode from SYS_Express_Source 
where CC_ID=
(select CC_ID from sys_caseCause where CC_Name='{0}' and allParentId like '%,{4},%'
and CC_NodeType =(select CC_NodeType from dbo.SYS_CaseCause_Type where CC_NodeTypeName='{1}')and XmlPath='{2}'
) and ES_Purpose=(   select AM_ID from [dbo].[SYS_Argument] where AM_Name='{3}') ", ccName, ccNodeTypeName, xmlPath, AMName, clickNodeTag);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 返回RegexInfo
        /// </summary>
        /// <param name="RegexCode"></param>
        /// <returns></returns>
        public DataTable GetRegexInfo(string RegexCode)
        {
            Database db = DatabaseFactory.CreateDatabase("LdtCommonManage");
            string sql = string.Format(@"select ER_Name,ER_RegexText from Reg_Regexs where ER_ID in(select ER_ID from Reg_InfoRegexRelation where EI_ID='{0}')", RegexCode);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// 获得争议焦点数据列表
        public DataTable GetArgumentFocus(string CC_ID)
        {
            string sql = string.Format(@" select SA_Id,SA_TextValue,SA_CaseNumber,SA_BookLogicalCode,SA_BookLogicalCode,case SA_CheckState when 0 then 'false' when 1 then 'true' else 'false' end as SA_CheckState,SA_TextID from SYS_ArgumentFocus where SA_RelevanceCaseID='{0}' ", CC_ID);
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 更新争议焦点数据表审核状态
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public bool UpdateArgumentFocusState(int checkState, string saId)
        {
            Database db = DatabaseFactory.CreateDatabase("JudgeDoc1");
            string sql = string.Format(@" update SYS_ArgumentFocus set SA_CheckState={0} where SA_Id='{1}' ", checkState, saId);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            int obj = db.ExecuteNonQuery(oCmd);
            if (obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// 获得已处理争议焦点数据列表
        public DataTable GetArgumentFocusDispose()
        {
            string sql = string.Format(@" select SA_Id, SA_TextValue, SA_RelevanceCaseID, SA_TextID from SYS_ArgumentFocus where SA_CheckState = 1 ");
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// 获得原有争议焦点数据列表
        public DataTable GetArgumentFocusDispose1()
        {
            string sql = string.Format(@"  select distinct SA_TextValue,SA_CaseID,SA_BookTypeCode,SA_BookLogicalCode,SA_TextID,SA_CaseNumber from SYS_ArgumentFocus ");
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// 获得原有争议焦点数据列表
        public DataTable GetArgumentFocusCaseID(string saTextID)
        {
            string sql = string.Format(@"  select SA_RelevanceCaseID from SYS_ArgumentFocus where SA_TextID='{0}' ", saTextID);
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// 增加争议焦点数据
        /// </summary>
        public bool AddArgumentFocus(ArgumentFocus model)
        {
            string sql = string.Format(@"INSERT INTO [Ldt_JudgeDocSystem].[dbo].[SYS_ArgumentFocus]
           ([SA_Id],[SA_CaseID],[SA_BookTypeCode],[SA_BookLogicalCode],[SA_TextValue]
           ,[SA_TextID],[SA_CaseNumber],[SA_RelevanceCaseID]
           ,[SA_UpdateTime],[SA_CheckState])
            VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}')", model.SA_Id, model.SA_CaseID, model.SA_BookTypeCode, model.SA_BookLogicalCode, model.SA_TextValue,
            model.SA_TextID, model.SA_CaseNumber, model.SA_RelevanceCaseID, DateTime.Now, model.SA_CheckState);
            Database db = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            int obj = db.ExecuteNonQuery(oCmd);
            if (obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// 删除争议焦点数据根据文本ID
        /// </summary>
        public bool DeleteArgumentFocusByTextID(string saTextID, string saCaseID)
        {
            string sql = string.Format(@" delete from SYS_ArgumentFocus where SA_TextID='{0}' and SA_CaseID='{1}' ", saTextID, saCaseID);
            Database db = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            int obj = db.ExecuteNonQuery(oCmd);
            if (obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// 获得原有争议焦点数据列表
        public DataTable GetArgumentFocusCase()
        {
            string sql = string.Format(@" select distinct SA_RelevanceCaseID from SYS_ArgumentFocus ");
            Database oBase = DatabaseFactory.CreateDatabase("JudgeDoc1");
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }



        /// <summary>
        /// 获取案由数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseCauseData()
        {
            string sql = string.Format(@" select CC_ID as ID,CC_Name as Name,CC_ParentID as ParentID from SYS_CaseCause where CC_Type=2 ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获取案由数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseCauseAllData()
        {
            string sql = string.Format(@" select CC_ID as ID,CC_Name as Name,CC_ParentID as ParentID from SYS_CaseCause  ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获取正确数据
        /// </summary>
        /// <returns></returns>
        public bool GetCurrentESID(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@"  select MS_ID from dbo.SYS_Express_MatchSituation
                                        where AllStituationIDs='{0}'  and SituationID = '{0}'
                                         and IsMatch =  1
                                        and AffirmIdeaResult=1 and SituationName like'%(%'", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                string esID = obj.ToString();
                sql = string.Format(@" delete from  SYS_Express_MatchSituation 
where SituationID = '{0}' 
and IsMatch = '1' 
and AffirmIdeaResult = '1'
 and AllStituationIDs = '{0}'
 and MS_ID<>'{1}'		", ccID, esID);
                oCmd = db.GetSqlStringCommand(sql.ToString());
                oCmd.CommandTimeout = 300;
                int obj1 = db.ExecuteNonQuery(oCmd);
                if (obj1 > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 获取一个节点下面的节点类型（去重并且不包含虚节点、XML节点、XML属性）
        /// </summary>
        /// <returns></returns>
        public DataTable GetCaseCauseNodeType(string ccId)
        {
            string sql = string.Format(@" select CC_NodeType,CC_NodeTypeName from SYS_CaseCause_Type where CC_NodeType in(
select  nodetype from
(select distinct CC_NodeType as nodetype  from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0 
union
select distinct CC_NodeSubType as nodetype from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0 
)a where a.nodetype not in(9,11,12)
)  ", ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 根据CCID获取下级节点类型 0原告 1被告 -1原告和被告
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public int GetNextNodeType(string ccID)
        {
            int i = -1;
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select CC_Name from [dbo].[SYS_CaseCause] where CC_ParentID='{0}' ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow item in table.Tables[0].Rows)
                {
                    if (item[0].ToString().Contains("原告"))
                    {
                        i = 0;
                    }
                    else if (item[0].ToString().Contains("被告"))
                    {
                        i = 1;
                    }
                }
            }
            return i;
        }

        /// <summary>
        /// 获取一个节点下面的节点类型（去重并且不包含虚节点、XML节点、XML属性）
        /// </summary>
        /// <returns></returns>
        public DataTable GetDistinctCaseCauseNodeType(string ccId)
        {
            string sql = string.Format(@" select distinct  CC_NodeType,CC_NodeSubType   from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0   ", ccId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataSet ds = oBase.ExecuteDataSet(oCmd);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取案由下的犯罪行为相关情形
        /// </summary>
        /// <param name="ccid"></param>
        /// <returns></returns>
        public string GetQXccID(string ccid)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select CC_ID from [dbo].[SYS_CaseCause] where CC_ParentID='{0}' and CC_NodeType=4 ", ccid);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取节点的所有父级
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string GetAllParentIdByCCID(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select AllParentId from SYS_CaseCause where CC_ID='{0}' ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取案由数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSysCaseDataByCCName(string ccName)
        {
            string sql = string.Format(@"    select   CC_ID as id,CC_Name as name,CC_ParentID, AllParentId,AllParentName from dbo.SYS_CaseCause  where CC_Name='{0}'   ", ccName);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取案由数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetAYCCName(string ccID)
        {
            string sql = string.Format(@"   select CC_ID  from SYS_CaseCause where AllParentId like '%,{0},%' and CC_NodeType = 2  ", ccID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 修改节点第二类别（保存xml批量修改用）
        /// </summary>
        /// <param name="ccid">所修改的节点</param>
        /// <param name="nodeSubType">第二类别类型</param>
        /// <returns></returns>
        public bool UpdateCCNodeSubTypes(string ccid, string nodeSubType)
        {
            string sql = string.Format(@" update SYS_CaseCause set CC_NodeSubType='{0}' where  CHARINDEX(',{1},',','+AllParentId+',')>0 and CC_Name<>'属性' ", nodeSubType, ccid);
            Database db = DatabaseFactory.CreateDatabase("JudgeDoc");
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            int obj = db.ExecuteNonQuery(oCmd);
            if (obj > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据案由ccid获取子级名称是否为案由要素，如果有案由要素则xpath提取从案由要素开始
        /// </summary>
        /// <param name="ccID"></param>
        /// <returns></returns>
        public string GetAyysIDByName(string ccID)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string sql = string.Format(@" select cc_id from sys_casecause where cc_parentid = '{0}'and cc_name = '案由要素' ", ccID);
            DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
            var obj = db.ExecuteScalar(oCmd);
            if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
            {
                return obj.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取行政执法中违法行为、违法情形的节点
        /// </summary>
        /// <returns></returns>
        public DataTable GetFCData(string ccId)
        {
            try
            {
                string sql = string.Format(@" select CC_ID,CC_Name,KeyWords   from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0 and  CC_NodeType in(29,32)  ", ccId);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询分词数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }

        /// <summary>
        /// 获取行政执法中违法行为、违法情形的节点
        /// </summary>
        /// <returns></returns>
        public DataTable GetXZZFData(string ccId)
        {
            try
            {
                string sql = string.Format(@" select CC_ID,CC_Name,KeyWords   from SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0 and  CC_NodeType in(29,32)  and KeyWords is not null  ", ccId);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询分词数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }

        /// <summary>
        /// 修改分词数据
        /// </summary>
        /// <param name="ccid"></param>
        /// <param name="keyWords"></param>
        /// <returns></returns>
        public bool UpdateKeyWords(string ccid, string keyWords)
        {
            try
            {
                string sql = $"  update SYS_CaseCause set KeyWords ='{keyWords}' where CC_ID='{ccid}' ";
                Database db = DatabaseFactory.CreateDatabase("JudgeDoc");
                DbCommand oCmd = db.GetSqlStringCommand(sql.ToString());
                int obj = db.ExecuteNonQuery(oCmd);
                if (obj > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("修改分词数据出错：" + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 获取行政执法相似违法行为数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetXZZFKeyWordByID(string ccId)
        {
            try
            {
                string sql = $@" Select b.CC_Name,b.KeyWords,case CCS_state  when 1 then 'true' else 'false' end as CCSRightState,case CCS_state  when 2 then 'true' else 'false' end as CCSWrongState,CCS_state,CCS_ID,a.CCS_SimilarID
from SYS_CaseCauseSimilar a,SYS_CaseCause b where a.CCS_SimilarID=b.CC_ID  and a.CC_ID='{ccId}' order by CCS_state  ";
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询分词数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }

        /// <summary>
        /// 修改关键词数据
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="PrimaryKeyWords"></param>
        /// <returns></returns>
        public bool UpCaseCausePrimaryKeyWords(string CC_ID, string PrimaryKeyWords)
        {
            string sql = $" update sys_caseCause set PrimaryKeyWords='{PrimaryKeyWords}' where CC_ID='{CC_ID}' ";

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取需要匹配的关联违法行为数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetPPData()
        {
            try
            {
                string sql = @" select PrimaryKeyWords, CC_ID from sys_caseCause where CHARINDEX(',66666,',','+AllParentId+',')>0 and CC_NodeType = 29 and PrimaryKeyWords<>'' ";
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询匹配的关联违法行为数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }

        /// <summary>
        /// 获取关联违法行为分组数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetCaseCauseRelation()
        {
            try
            {
                string sql = "  select a.*,b.PrimaryKeyWords from SYS_CaseCauseRelation a  join sys_caseCause b on a.CC_ID=b.CC_ID ";
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询关联违法行为分组数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }


        /// <summary>
        /// 删除关联违法行为数据根据关联分组
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="PrimaryKeyWords"></param>
        /// <returns></returns>
        public bool UpdateSYSCaseCauseRelationByGroupID(string CCR_GroupID)
        {
            string sql = $" delete from SYS_CaseCauseRelation where CCR_GroupID='{CCR_GroupID}' ";

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除关联违法行为数据根据CCID
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="PrimaryKeyWords"></param>
        /// <returns></returns>
        public bool UpdateSYSCaseCauseRelationByCCID(string CC_ID, string CCR_GroupID)
        {
            string sql = $" delete from SYS_CaseCauseRelation where CCR_GroupID='{CCR_GroupID}' and CC_ID = '{CC_ID}' ";

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 插入关联违法行为数据
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="PrimaryKeyWords"></param>
        /// <returns></returns>
        public bool InsertSYSCaseCauseRelation(SYS_CaseCauseRelation sccr)
        {
            string sql = $" insert into dbo.SYS_CaseCauseRelation(CCR_ID, CCR_GroupID, CC_ID,UpdateTime,CCR_State) values('{sccr.CCR_ID}','{sccr.CCR_GroupID}','{sccr.CC_ID}','{sccr.UpdateTime}',{sccr.CCR_State}) ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取关联违法行为分组数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetCaseCauseRelationData(string ccID)
        {
            try
            {
                string sql = $"  select * from SYS_CaseCauseRelation where CC_ID='{ccID}' ";
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询关联违法行为分组数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }


        /// <summary>
        /// 插入关联违法行为数据
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="PrimaryKeyWords"></param>
        /// <returns></returns>
        public bool UpdateSYSCaseCauseRelation(SYS_CaseCauseRelation sccr)
        {
            string sql = $" update SYS_CaseCauseRelation set CCR_GroupID = '{sccr.CCR_GroupID}', UpdateTime = '{sccr.UpdateTime}' where CCR_ID = '{sccr.CCR_ID}' ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取关联违法行为分组数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetCaseCauseRelationData()
        {
            try
            {
                string sql = @" select b.PrimaryKeyWords as gjc,b.CC_Name as glfwxw,b.AllParentName, a.*  from SYS_CaseCauseRelation a  join sys_caseCause b on a.CC_ID=b.CC_ID  order by CCR_GroupID ";
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询关联违法行为分组数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }


        /// <summary>
        /// 修改数据审核状态
        /// </summary>
        /// <param name="CC_ID"></param>
        /// <param name="PrimaryKeyWords"></param>
        /// <returns></returns>
        public bool UpdateSYSCaseCauseRelationState(string ccrId, int state)
        {
            string sql = $" update SYS_CaseCauseRelation set CCR_State = {state}, UpdateTime = GETDATE() where CCR_ID = '{ccrId}' ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改父级及所有子级的地域
        /// </summary>
        /// <param name="ccId"></param>
        /// <param name="sysDistrict"></param>
        /// <returns></returns>
        public bool UpdateSysDistrict(string ccId, string sysDistrict)
        {
            string sql = $" update SYS_CaseCause set CC_District = '{sysDistrict}' where CC_ID in(select CC_ID from SYS_CaseCause where CHARINDEX(',{ccId},',','+AllParentId+',')>0) ";
            return SqlManage.ExecuteNonQuerySql(sql, DbName);
        }

        /// <summary>
        /// 获取法条ID数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetQuoteLawRelData(string ccId)
        {
            try
            {
                string sql = $" select OLR_Law_ProvID from SYS_QuoteLawRel where QLR_ProvType = 2 and OLR_SituationID = '{ccId}' ";
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                DataSet ds = oBase.ExecuteDataSet(oCmd);
                if (ds != null && ds.Tables.Count > 0)
                {
                    return ds.Tables[0];
                }
                else
                {
                    return new DataTable();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询关联违法行为分组数据出错：" + ex.Message);
                return new DataTable(); ;
            }
        }

        /// <summary>
        /// 获取节点的所有父级根据地域条件
        /// </summary>
        /// <param name="ccids">节点ccids</param>
        /// <returns></returns>
        public List<string> GetAllParentIdByDistrictCondition(List<string> ccids)
        {
           // string sql = $"  select AllParentId from SYS_CaseCause where {condition} ";
            return SqlManage.sqlServerJudgeDocSystem.Select<SYS_CaseCause>().Where(x => ccids.Contains(x.CC_ID)).ToList(x =>x.AllParentId);
        }

        /// <summary>
        /// 根据查询条件获取CCID
        /// </summary>
        /// <param name="condition">条件id</param>
        /// <param name="type">1:EIid 2:RIUid 3:ERid</param>
        /// <returns>返回根据条件查询到的ccid</returns>
        public string GetCCIdBySearchCondition(string condition, int type)
        {
            string ccId = string.Empty;
            string CC_RegexInfoCode = string.Empty;
            switch (type)
            {
                case 1:
                    ccId = SqlManage.sqlServerJudgeDocSystem.Select<SYS_CaseCause, SysExpressSource>().InnerJoin((a, b) => a.CC_ID == b.CC_ID).Where((a, b) => b.CC_RegexInfoCode == condition)?.First()?.CC_ID;
                    break;
                case 2:
                     CC_RegexInfoCode = SqlManage.sqlServerCommonManage.Select<RuleInfoUnit>().Where(x => x.RIU_ID == condition)?.First()?.RIU_Code;
                    if (!string.IsNullOrEmpty(CC_RegexInfoCode))
                        ccId = GetCCIdBySearchCondition(CC_RegexInfoCode, 1);
                    break;
                case 3:
                   CC_RegexInfoCode=SqlManage.sqlServerCommonManage.Select<RuleInfoUnit, Riu_RuleRelation>().InnerJoin((a, b) => a.RIU_ID == b.RIU_ID).Where((a, b) => b.RR_RelationTable_ID == condition)?.First()?.RIU_Code;
                    if (!string.IsNullOrEmpty(CC_RegexInfoCode))
                        ccId = GetCCIdBySearchCondition(CC_RegexInfoCode, 1);
                    break;
            }
            return ccId;
        }
    }
}
